using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;

namespace PackTracker.Business
{ 
	[Serializable()] 
	public class ItemMonitoramentoCollection : Csla.BusinessListBase<ItemMonitoramentoCollection, ItemMonitoramento>
	{
		#region BindingList Overrides
		protected override object AddNewCore()
		{
			ItemMonitoramento item = ItemMonitoramento.NewItemMonitoramento();
			this.Add(item);
			return item;
		}
		#endregion //BindingList Overrides

		#region Authorization Rules

		public static bool CanGetObject()
		{
			//TODO: Define CanGetObject permission in ItemMonitoramentoCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ItemMonitoramentoCollectionViewGroup"))
			//	return true;
			//return false;
		}

		public static bool CanAddObject()
		{
			//TODO: Define CanAddObject permission in ItemMonitoramentoCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ItemMonitoramentoCollectionAddGroup"))
			//	return true;
			//return false;
		}

		public static bool CanEditObject()
		{
			//TODO: Define CanEditObject permission in ItemMonitoramentoCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ItemMonitoramentoCollectionEditGroup"))
			//	return true;
			//return false;
		}

		public static bool CanDeleteObject()
		{
			//TODO: Define CanDeleteObject permission in ItemMonitoramentoCollection
			return true;
			//if (Csla.ApplicationContext.User.IsInRole("ItemMonitoramentoCollectionDeleteGroup"))
			//	return true;
			//return false;
		}
		#endregion //Authorization Rules

		#region Factory Methods
		private ItemMonitoramentoCollection()
		{ /* require use of factory method */ }

		private ItemMonitoramentoCollection(SafeDataReader dr)
		{
			MarkAsChild();
			Fetch(dr);
		}

		public static ItemMonitoramentoCollection NewItemMonitoramentoCollection()
		{
			if (!CanAddObject())
				throw new System.Security.SecurityException("User not authorized to add a ItemMonitoramentoCollection");
			return new ItemMonitoramentoCollection();
		}

		public static ItemMonitoramentoCollection GetItemMonitoramentoCollection()
		{
			if (!CanGetObject())
				throw new System.Security.SecurityException("User not authorized to view a ItemMonitoramentoCollection");
			return DataPortal.Fetch<ItemMonitoramentoCollection>(new FilterCriteria());
		}

		internal static ItemMonitoramentoCollection GetItemMonitoramentoCollection(SafeDataReader dr)
		{
			return new ItemMonitoramentoCollection(dr);
		}

		/*
			public static ItemMonitoramentoCollection GetItemMonitoramentoCollection(int idMonitoramento, int idItemPedido)
			{
				if (!CanGetObject())
					throw new System.Security.SecurityException("User not authorized to view a ItemMonitoramentoCollection");
				return DataPortal.Fetch<ItemMonitoramentoCollection>(new FilterCriteria(idMonitoramento, idItemPedido));
			}
		*/

		#endregion //Factory Methods

		#region Data Access

		#region Filter Criteria
		[Serializable()]
		private class FilterCriteria
		{
			public int IdMonitoramento;
			public int IdItemPedido;

			public FilterCriteria()
			{
			}

			public FilterCriteria(int idMonitoramento, int idItemPedido)
			{
				this.IdMonitoramento = idMonitoramento;
				this.IdItemPedido = idItemPedido;
			}
		}
		#endregion //Filter Criteria

		#region Data Access - Fetch
		private void DataPortal_Fetch(FilterCriteria criteria)
		{
			RaiseListChangedEvents = false;

			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				ExecuteFetch(cn, criteria);
			}//using

			RaiseListChangedEvents = true;
		}

		private void ExecuteFetch(SqlConnection cn, FilterCriteria criteria)
		{
			using (SqlCommand cm = cn.CreateCommand())
			{
				cm.CommandType = CommandType.StoredProcedure;
				cm.CommandText = "pr_GetItemMonitoramentoCollection";
				/*
				cm.Parameters.AddWithValue("@idMonitoramento", criteria.IdMonitoramento);
				cm.Parameters.AddWithValue("@idItemPedido", criteria.IdItemPedido);
				*/

				using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
				{
					Fetch (dr);
				}
			}//using
		}

		private void Fetch(SafeDataReader dr)
		{
			while (dr.Read())
				this.Add(ItemMonitoramento.GetItemMonitoramento(dr));
		}

		#endregion //Data Access - Fetch

		#region Data Access - Update
		protected override void DataPortal_Update()
		{

			using (SqlConnection cn = new SqlConnection(Database.DefaultConnection))
			{
				cn.Open();

				Update(cn);

			}//using SqlConnection
		}

		internal void Update(SqlConnection cn)
		{
			RaiseListChangedEvents = false;

			// loop through each deleted child object
			foreach (ItemMonitoramento deletedChild in DeletedList)
				deletedChild.DeleteSelf(cn);
			DeletedList.Clear();

			// loop through each non-deleted child object
			foreach (ItemMonitoramento child in this)
			{
				if (child.IsNew)
					child.Insert(cn);
				else
					child.Update(cn);
			}

			RaiseListChangedEvents = true;
		}

		#endregion //Data Access - Update
		#endregion //Data Access
	}
}
